package com.lzlz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzlz.common.result.Result;
import com.lzlz.common.utils.MD5;
import com.lzlz.model.hosp.HospitalSet;
import com.lzlz.service.HospitalSetService;
import com.lzlz.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(value = "医院设置管理")
@RestController
@RequestMapping("admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetContorller {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1查询医院设置表
    // 所有信息；
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospiatlSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return  Result.ok(list);
    }
//2 删除医院设置
@ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //3条件查询带分页
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPageHosp/{current}/{limit}")
     public  Result findPageHosp(@PathVariable long current, @PathVariable long limit
                                   ,@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象 传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //调用方法
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (StringUtils.isNotBlank(hosname)){
            wrapper.like(StringUtils.isNotBlank(hospitalSetQueryVo.getHosname()),HospitalSet::getHosname,hospitalSetQueryVo.getHosname());
        }
        if (StringUtils.isNotBlank(hoscode)){
            wrapper.eq(StringUtils.isNotBlank(hospitalSetQueryVo.getHoscode()),HospitalSet::getHoscode,hospitalSetQueryVo.getHoscode());
        }
        Page<HospitalSet> pageHospSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospSet);
    }
    //4 添加医院设置

    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1 使用 0不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+ random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //5 根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    public Result getHospSet (@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return  Result.ok(hospitalSet);
    }

    // 6 修改医院设置
    @PostMapping("updataHospitalSet")
    public  Result updataHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //7 批量医院接口
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idlist ){
        boolean b = hospitalSetService.removeByIds(idlist);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //8 医院设置锁定与解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public  Result lockHospitalSet(@PathVariable long id,@PathVariable Integer status){
       //根据id查询医院信息 设置状态
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);

        //调用方法
        hospitalSetService.updateById(hospitalSet);

        return  Result.ok();

    }
    //9 发送签名key(token) 秘钥
    @PutMapping("sendKey/{id}")
    public  Result lockHospitalSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //发送短信
        return  Result.ok();
    }


}















